package com.basic.tools.codegen.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.basic.tools.codegen.common.Action;
import com.basic.tools.codegen.common.Result;
import com.basic.tools.codegen.entity.SysDictData;
import com.basic.tools.codegen.entity.SysDictType;
import com.basic.tools.codegen.entity.domain.StringUtils;
import com.basic.tools.codegen.mapper.SysDictDataMapper;
import com.basic.tools.codegen.mapper.SysDictTypeMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("dict")
@Api(tags = "字典管理")
public class DictController {

    @Autowired
    SysDictDataMapper sysDictDataMapper;

    @Autowired
    SysDictTypeMapper sysDictTypeMapper;

    @ApiOperation(value = "字典类型-列表", notes = "字典类型-列表")
    @PostMapping("/getDictType")
    public Result getDictType(@RequestBody SysDictType sysDictType) {
        QueryWrapper<SysDictType> queryWrapper = new QueryWrapper<>();
        String dictName = sysDictType.getDictName();
        if (StringUtils.isNotBlank(dictName)) {
            queryWrapper.like("dict_name", dictName);
        }
        return Action.ok(sysDictTypeMapper.selectList(queryWrapper));
    }

    @ApiOperation(value = "字典类型-添加", notes = "字典类型-添加")
    @PostMapping("/addDictType")
    public Result addDictType(@RequestBody SysDictType sysDictType) {
        sysDictTypeMapper.insert(sysDictType);
        return Action.ok();
    }

    @ApiOperation(value = "字典类型-修改", notes = "字典类型-修改")
    @PostMapping("/updateDictType")
    public Result updateDictType(@RequestBody SysDictType sysDictType) {
        sysDictTypeMapper.updateById(sysDictType);
        return Action.ok();
    }

    @ApiOperation(value = "字典类型-删除", notes = "字典类型-删除")
    @PostMapping("/deleteDictType")
    public Result deleteDictType(@RequestBody SysDictType sysDictType) {
        QueryWrapper<SysDictType> qw=new QueryWrapper<>();
        qw.eq("dict_id",sysDictType.getDictId());
        sysDictTypeMapper.delete(qw);
        Map<String,Object> kv=new HashMap<String,Object>();
        kv.put("dict_id",sysDictType.getDictId());
        sysDictTypeMapper.deleteByMap(kv);
        sysDictType.deleteById();
        return Action.ok(sysDictTypeMapper.deleteById(sysDictType.getDictId()));
    }

    @ApiOperation(value = "字典数据-列表", notes = "字典数据-列表")
    @PostMapping("/getDictData")
    public Result getDictData(@RequestBody SysDictData sysDictData) {
        String dictType = sysDictData.getDictType();
        String dictLabel = sysDictData.getDictLabel();
        String dictValue = sysDictData.getDictValue();
        QueryWrapper<SysDictData> qw = new QueryWrapper();
        if(StringUtils.isNotBlank(dictType)){
            qw.eq("dict_type",dictType);
        }
        if(StringUtils.isNotBlank(dictLabel)){
            qw.like("dict_label",dictLabel);
        }
        if(StringUtils.isNotBlank(dictValue)){
            qw.like("dict_value",dictValue);
        }

        return Action.ok(sysDictDataMapper.selectList(qw));
    }

    @ApiOperation(value = "字典数据-添加", notes = "字典类型-添加")
    @PostMapping("/addDictData")
    public Result addDictData(@RequestBody SysDictData sysDictData) {
        sysDictDataMapper.insert(sysDictData);
        return Action.ok();
    }

    @ApiOperation(value = "字典数据-修改", notes = "字典类型-修改")
    @PostMapping("/updateDictData")
    public Result updateDictData(@RequestBody SysDictData sysDictData) {
        sysDictDataMapper.updateById(sysDictData);
        return Action.ok();
    }

    @ApiOperation(value = "字典数据-删除", notes = "字典类型-删除")
    @PostMapping("/deleteDictData")
    public Result deleteDictData(@RequestBody SysDictData sysDictData) {
        QueryWrapper<SysDictData> qw=new QueryWrapper<>();
        qw.eq("dict_code",sysDictData.getDictCode());
        sysDictDataMapper.delete(qw);
        return Action.ok();
    }


}
